package com.shelpe.services.lexicon.service.tag;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.stereotype.Service;

import com.shelpe.services.lexicon.entity.KeywordRecommend;
import com.shelpe.services.lexicon.entity.KeywordTag;

@Service
/**
 * 热词包规则
 * @author Otis
 *
 */
public class HotRecommendTagRule implements RecommendTagRule {
	
	@Override
	public KeywordTag tag(KeywordRecommend word,TagRuleContext context) {
		KeywordTag tag = null;
		
		boolean satisfied = false;
		
		if(context.getRecommends() != null){
			List<KeywordRecommend> recommends = new ArrayList<KeywordRecommend>(context.getRecommends().size() * 2);
			for(KeywordRecommend recommend: context.getRecommends()){
				recommends.add(recommend);
			}
			Collections.sort(recommends, new Comparator<KeywordRecommend>(){
				@Override
				public int compare(KeywordRecommend o1, KeywordRecommend o2) {
					int c1 = o1.getInsight()!=null?o1.getInsight().getImpression():0;
					int c2 = o2.getInsight()!=null?o2.getInsight().getImpression():0;
					return c2 - c1;
				}
			});
			int index = -1;
			for(KeywordRecommend km:recommends){
				if(km.getWord().equals(word.getWord())){
					index = recommends.indexOf(km);
					break;
				}
			}
			if(index > -1 && (index * 100 / recommends.size()) < 30){
				satisfied = true;
			}
		}
		
		if(satisfied){
			tag = new KeywordTag();
			tag.setTagID(3);
			tag.setTagName("hot");
		}
		return tag;
	}
	
}
